package loadbalance;

import java.util.logging.Logger;

public class ServiceInvoker {

    private static final ServiceInvoker INSTANCE = new ServiceInvoker();

    private ServiceInvoker() {
    }

    public static ServiceInvoker getInstance() {
        return INSTANCE;
    }

    private volatile LoadBalancer loadBalancer;

    public LoadBalancer getLoadBalancer() {
        return loadBalancer;
    }

    public void setLoadBalancer(LoadBalancer loadBalancer) {
        this.loadBalancer = loadBalancer;
    }

    /**
     * 根据指定的负载均衡器派发请求到特定的下游部件。
     *
     * @param request
     *          待派发的请求
     */
    public void dispatchRequest(Request request) {
        // 这里读取volatile变量loadBalancer
        Endpoint endpoint = getLoadBalancer().nextEndpoint();

        if (null == endpoint) {
            // 省略其他代码

            return;
        }

        // 将请求发给下游部件
        dispatchToDownstream(request, endpoint);

    }

    // 真正将指定的请求派发给下游部件
    private void dispatchToDownstream(Request request, Endpoint endpoint) {
        System.out.println("Dispatch request to " + endpoint + ":" + request);
        // 省略其他代码
    }
}
